package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import com.zzyl.nursing.dto.NursingAlertDataListDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author laoYe
 * @date 2025-11-01
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param nursingAlertDataListDto 报警数据
     * @return 报警数据
     */
    @Override
    public TableDataInfo<AlertData> selectAlertDataList(NursingAlertDataListDto nursingAlertDataListDto) {
        Page<AlertData> pageHelper = new Page<>(nursingAlertDataListDto.getPageNum(), nursingAlertDataListDto.getPageSize());
        lambdaQuery().eq(StringUtils.isNotEmpty(nursingAlertDataListDto.getDeviceName()),AlertData::getDeviceName, nursingAlertDataListDto.getDeviceName())
                        .ge(!ObjectUtils.isEmpty(nursingAlertDataListDto.getStartTime()),AlertData::getCreateTime,nursingAlertDataListDto.getStartTime())
                        .le(!ObjectUtils.isEmpty(nursingAlertDataListDto.getEndTime()),AlertData::getCreateTime,nursingAlertDataListDto.getEndTime())
                .eq(!ObjectUtils.isEmpty(nursingAlertDataListDto.getStatus()),AlertData::getStatus, nursingAlertDataListDto.getStatus())
                .page(pageHelper);
        TableDataInfo<AlertData> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(pageHelper.getTotal());
        tableDataInfo.setRows(pageHelper.getRecords());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("请求成功");
        return tableDataInfo;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理报警数据
     * @param handleAlertDataDto
     */
    @Override
    public void handleAlertData(HandleAlertDataDto handleAlertDataDto) {
        LambdaUpdateWrapper<AlertData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AlertData::getId, handleAlertDataDto.getId());
        updateWrapper.set(AlertData::getProcessingResult, handleAlertDataDto.getProcessingResult());
        updateWrapper.set(AlertData::getProcessingTime,handleAlertDataDto.getProcessingTime());
        update(updateWrapper);
    }
}
